package org.example.examsystem.service;



import org.example.examsystem.entity.Subject;
import org.example.examsystem.mapper.SubjectMapper;
import org.example.examsystem.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService {

    @Autowired
    private SubjectMapper subjectMapper;


    @Override
    public Page<Subject> getQuestionsByPage(int page, int size, int stype) {
        // 计算 offset
        int offset = (page - 1) * size;
        List<Subject> questions = subjectMapper.getQuestionsByPageWithCname(offset, size, stype);
        int totalCount = subjectMapper.getTotalCount(stype);
        return new PageImpl<>(questions, Pageable.ofSize(size).withPage(page - 1), totalCount);
    }


    @Override
    public void insertQuestion(Subject subject) {
        subject.setStype(subject.getStype()); // 确保插入的是单选题
        subjectMapper.insertQuestion(subject);
    }

    @Override
    public void updateQuestion(Subject subject) {
        subjectMapper.updateQuestion(subject);
    }

    @Override
    public void deleteQuestion(int sid) {
        subjectMapper.deleteQuestion(sid);
    }

    @Override
    public void batchDeleteQuestions(List<Integer> ids) {
        subjectMapper.batchDeleteQuestions(ids);
    }

    @Override
    public List<Subject> getRandomSubjects(int cno, int stype, int count) {
        return subjectMapper.getRandomSubjects(cno, stype, count);
    }

    @Override
    public Subject getSubjectById(int sid) {
        return subjectMapper.getSubjectById(sid);
    }
}